package com.xzwk.subject.domain.servcie.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xzwk.subject.domain.convert.SubjectLabelConverter;
import com.xzwk.subject.domain.dto.SubjectLabelDTO;
import com.xzwk.subject.domain.entity.SubjectLabelBO;
import com.xzwk.subject.domain.servcie.SubjectLabelDomainService;
import com.xzwk.subject.infra.basic.entity.SubjectLabel;
import com.xzwk.subject.infra.basic.entity.SubjectMapping;
import com.xzwk.subject.infra.basic.service.SubjectLabelService;
import com.xzwk.subject.infra.basic.service.SubjectMappingService;
import java.io.Serializable;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

@Service
public class SubjectLabelDomainServiceImpl implements SubjectLabelDomainService {

  @Resource
  private SubjectLabelService subjectLabelService;
  @Resource
  private SubjectMappingService subjectMappingService;
  @Resource
  private SubjectLabelConverter subjectLabelConverter;

  @Override
  public void add(SubjectLabelBO subjectLabelBO) {

    SubjectLabel subjectLabel= subjectLabelConverter.toSubjectLabel(subjectLabelBO);
    subjectLabelService.save(subjectLabel);
  }

  @Override
  public Object page(Page<SubjectLabel> page,
      QueryWrapper<SubjectLabel> subjectLabelQueryWrapper) {
    return null;
  }

  @Override
  public SubjectLabelDTO getById(Serializable id) {
    SubjectLabel subjectLabel = subjectLabelService.getById(id);
    return subjectLabelConverter.toSubjectLabelDTO(subjectLabel);
  }

  @Override
  public Boolean save(SubjectLabelBO subjectLabelBO) {
    SubjectLabel subjectLabel= subjectLabelConverter.toSubjectLabel(subjectLabelBO);
    subjectLabel.setCreatedTime(new Date());
    return subjectLabelService.save(subjectLabel);
  }

  @Override
  public Boolean updateById(SubjectLabelBO subjectLabelBO) {
    SubjectLabel subjectLabel= subjectLabelConverter.toSubjectLabel(subjectLabelBO);
    subjectLabel.setUpdateTime(new Date());
    return subjectLabelService.updateById(subjectLabel);
  }

  @Override
  public Boolean removeByIds(List<Long> idList) {
    return subjectLabelService.removeByIds(idList);
  }

  @Override
  public List<SubjectLabelBO> queryPrimaryLabel(Long parentId) {
    return null;
  }

  @Override
  public List<SubjectLabelDTO> queryLabelByCategoryId(Long categoryId) {
    List<SubjectMapping> subjectMappingList= subjectMappingService.queryMappingByCategoryId(categoryId);

    if (CollectionUtils.isEmpty(subjectMappingList)){
      return Collections.emptyList();
    }
    List<Long> labelIdList = subjectMappingList.stream().map(SubjectMapping::getLabelId)
        .collect(Collectors.toList());

    List<SubjectLabel> subjectLabelList = subjectLabelService.selectBatchById(labelIdList);
    return subjectLabelConverter.toSubjectLabelDTO(subjectLabelList);
  }
}
